package de.teamgamma.cansat.groundstation.export;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import de.teamgamma.cansat.groundstation.livestream.Livestream;
import de.teamgamma.cansat.groundstation.livestream.LivestreamData;
import de.teamgamma.cansat.groundstation.livestream.TransmittedValue;

/**
 * Object that contains exportable data
 * @author Marc Huisinga
 *
 */
public class ExportableData extends LivestreamData {
	private File inputFile;
	private String[] keys;
	private String[] units;
	private String[] gpsKeys;
	private TransmittedValue[] values;
	private String nameOfInputFile;
	private LivestreamData gpsData;

	public ExportableData() {
		gpsData = new LivestreamData();
	}
	
	/**
	 * Imports the data into the ExportableData object
	 * @param input File from where to import
	 */
	public void importData(File input) {
		try {
			BufferedReader reader = new BufferedReader(new FileReader(input));
			inputFile = input;
			nameOfInputFile = inputFile.getName();
			if (nameOfInputFile.indexOf(".") != -1) {
				nameOfInputFile = nameOfInputFile.substring(0, nameOfInputFile.lastIndexOf("."));
			}
			String currentLine = reader.readLine();
			String[] header = currentLine.split(String.valueOf(Livestream.DATASEPARATOR));
			keys = new String[header.length];
			units = new String[header.length];
			values = new TransmittedValue[header.length];
			int dataIndex = 0; 
			for (String currentElem : header) {
				String[] elemData = currentElem.split(String.valueOf(Livestream.UNITSEPARATOR));
				keys[dataIndex] = elemData[0];
				units[dataIndex] = elemData[1];
				values[dataIndex] = new TransmittedValue(elemData[0], elemData[1]);
				dataIndex ++;
			}
			currentLine = reader.readLine();
			dataIndex = 0;
			ArrayList<String> gpsKeys = new ArrayList<String>();
			while (currentLine != null) {
				String[] dataOfLine = currentLine.split(String.valueOf(Livestream.DATASEPARATOR));
				for (String currentKey : keys) {
					try {
						Double.valueOf(dataOfLine[dataIndex]);
						addDataToKey(currentKey, dataOfLine[dataIndex]);
					} catch(NumberFormatException e) {
						gpsData.addDataToKey(currentKey, dataOfLine[dataIndex]);
						if (!gpsKeys.contains(currentKey)) {
							gpsKeys.add(currentKey);
						}
					}
					dataIndex ++;
				}
				dataIndex = 0;
				currentLine = reader.readLine();
			}
			reader.close();
			String[] keysWithoutGpsKeys = new String[keys.length-gpsKeys.size()];
			TransmittedValue[] valuesWithoutGpsValues = new TransmittedValue[values.length-gpsKeys.size()];
			String[] unitsWithoutGpsUnits = new String[units.length-gpsKeys.size()];
			for (int i = 0, withoutGpsIndex = 0; i != keys.length; i++) {
				if (!gpsKeys.contains(keys[i])) {
					keysWithoutGpsKeys[withoutGpsIndex] = keys[i];
					valuesWithoutGpsValues[withoutGpsIndex] = values[i];
					unitsWithoutGpsUnits[withoutGpsIndex] = units[i];
					withoutGpsIndex ++;
				}
			}
			keys = keysWithoutGpsKeys;
			values = valuesWithoutGpsValues;
			units = unitsWithoutGpsUnits;
			this.gpsKeys = new String[gpsKeys.size()];
			for (int i = 0; i != gpsKeys.size(); i++) {
				this.gpsKeys[i] = gpsKeys.get(i);
			}
		} catch (FileNotFoundException e) {
			System.err.println("File not found!");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Gets the keys that got imported
	 * @return imported keys
	 */
	public String[] getKeys() {
		return keys;
	}

	/**
	 * Gets the file from where the data got imported
	 * @return file from where the data got imported
	 */
	public File getInputFile() {
		return inputFile;
	}

	/**
	 * Gets the original name of the input file
	 * @return name
	 */
	public String getNameOfInputFile() {
		return nameOfInputFile;
	}

	/**
	 * Gets the TransmittedValues that got imported
	 * @return values
	 */
	public TransmittedValue[] getValues() {
		return values;
	}

	/**
	 * Gets the units that got imported
	 * @return units
	 */
	public String[] getUnits() {
		return units;
	}

	/**
	 * Gets the logged gps data.
	 * @return gps data
	 */
	public LivestreamData getGpsData() {
		return gpsData;
	}
}
